package cn.tedu.loding.rope.iml;

import cn.tedu.loding.pojo.entity.Attribute;
import cn.tedu.loding.pojo.vo.AttributeListVO;
import cn.tedu.loding.pojo.vo.AttributeStandardVO;
import cn.tedu.loding.rope.IAttributeRedisRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@Repository
public class AttributeRedisRepositoryImpl implements IAttributeRedisRepository {
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;



    public AttributeRedisRepositoryImpl() {
        log.debug("创建处理缓存的数据访问对象：AttributeRedisRepositoryImpl");
    }


    @Override
    public void save(AttributeStandardVO attributeStandardVO) {
        String key = ATTRIBUTE_ITEM_KEY_PREFIX + attributeStandardVO.getId();
        redisTemplate.opsForSet().add(ATTRIBUTE_ITEM_KEYS_KEY,key);
        redisTemplate.opsForValue().set(key,attributeStandardVO);
    }

    @Override
    public void save(List<AttributeListVO> attributes) {
        String key = ATTRIBUTE_LIST_KEY;
        ListOperations<String,Serializable> ops = redisTemplate.opsForList();
        for (AttributeListVO attributeListVO:attributes) {
            ops.rightPush(key,attributeListVO);
        }
    }

    @Override
    public Long deleteAll() {
        //获取到所有item的Key
        Set<Serializable> members = redisTemplate
                .opsForSet().members(ATTRIBUTE_ITEM_KEYS_KEY);
        Set<String> keys = new HashSet<>();
        for (Serializable member: members) {
            keys.add((String) member);
        }
        //将List和保存key的Set的key也添加到集合中
        keys.add(ATTRIBUTE_LIST_KEY);
        keys.add(ATTRIBUTE_ITEM_KEYS_KEY);
        return redisTemplate.delete(keys);
    }

    @Override
    public AttributeStandardVO get(Long id) {
        Serializable serializable = redisTemplate
                .opsForValue().get(ATTRIBUTE_ITEM_KEY_PREFIX + id);
        AttributeStandardVO attributeStandardVO = null;
        if (serializable != null) {
            if (serializable instanceof AttributeStandardVO) {
                attributeStandardVO = (AttributeStandardVO) serializable;
            }
        }
        return attributeStandardVO;
    }

    @Override
    public List<AttributeListVO> list() {
        long start = 0;
        long end = -1;
        return list(start, end);
    }

    @Override
    public List<AttributeListVO> list(Long start, Long end) {
        String key = ATTRIBUTE_LIST_KEY;
        ListOperations<String, Serializable> ops = redisTemplate.opsForList();
        List<Serializable> list = ops.range(key, start, end);
        List<AttributeListVO> brands = new ArrayList<>();
        for (Serializable item : list) {
            brands.add((AttributeListVO) item);
        }
        return brands;
    }


}
